package com.xdjmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xdjmall.dao.IndexConfigDao;
import com.xdjmall.entity.IndexConfig;
import com.xdjmall.util.DBUtils;
import com.xdjmall.util.QueryCondition;
import com.xdjmall.util.SQLHandler;

public class IndexConfigDaoImpl implements IndexConfigDao {

	@Override
	public Integer insert(IndexConfig i) throws SQLException {
		// TODO 德龙
		String sql = "insert into index_config(config_name,config_type,goods_id,redirect_url,config_rank,is_deleted,create_time,create_user,update_time,update_user) values(?,?,?,?,?,?,?,?,?,?)";
		try (Connection c = DBUtils.getConnection();
			PreparedStatement ps = c.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS)) {
			ps.setString(1, i.getConfigName());
			ps.setInt(2, i.getConfigType());
			ps.setInt(3, i.getGoodsId());
			ps.setString(4, i.getRedirectUrl());
			ps.setInt(5, i.getConfigRank());
			ps.setBoolean(6, i.getIsDeleted());
			ps.setObject(7, i.getCreateTime());
			ps.setInt(8, i.getCreateUser());
			ps.setObject(9, i.getUpdateTime());
			ps.setInt(10, i.getUpdateUser());
			int row = ps.executeUpdate();
			ResultSet keySet = ps.getGeneratedKeys();
			if (keySet.next()) {
				i.setConfigId(1);
			}
			return row;
		}
	}

	@Override
	public Integer delete(Integer id) throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public Integer update(IndexConfig i) throws SQLException {
		// TODO 德龙
		String sql = "update index_config set ";
		if (i.getConfigName() != null) {
			sql += " config_name=?,";
		}
		if (i.getConfigType() != null) {
			sql += " config_type=?,";
		}
		if (i.getGoodsId() != null) {
			sql += " goods_id=?,";
		}
		if (i.getRedirectUrl() != null) {
			sql += " redirect_url=?,";
		}
		if (i.getConfigRank() != null) {
			sql += " config_rank=?,";
		}
		if (i.getIsDeleted() != null) {
			sql += " is_deleted=?,";
		}
		if (i.getCreateTime() != null) {
			sql += " create_time=?,";
		}
		if (i.getCreateUser() != null) {
			sql += " create_user=?,";
		}
		if (i.getUpdateTime() != null) {
			sql += " update_time=?,";
		}
		if (i.getUpdateUser() != null) {
			sql += " update_user=?,";
		}
		// 截取并删除逗号 substring 包前不包后
		sql += " update_time=now() where config_id=?";
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			int num = 1;
			if (i.getConfigName() != null) {
				ps.setString(num++, i.getConfigName());
			}
			if (i.getConfigType() != null) {
				ps.setInt(num++, i.getConfigType());
			}
			if (i.getGoodsId() != null) {
				ps.setInt(num++, i.getGoodsId());
			}
			if (i.getRedirectUrl() != null) {
				ps.setString(num++, i.getRedirectUrl());
			}
			if (i.getConfigRank() != null) {
				ps.setInt(num++, i.getConfigRank());
			}
			if (i.getIsDeleted() != null) {
				ps.setBoolean(num++, i.getIsDeleted());
			}
			if (i.getCreateTime() != null) {
				ps.setObject(num++, i.getCreateTime());
			}
			if (i.getCreateUser() != null) {
				ps.setInt(num++, i.getCreateUser());
			}
			if (i.getUpdateTime() != null) {
				ps.setObject(num++, i.getUpdateTime());
			}
			if (i.getUpdateUser() != null) {
				ps.setInt(num++, i.getUpdateUser());
			}
			ps.setInt(num, i.getConfigId());
			return ps.executeUpdate();
		}
	}

	@Override
	public IndexConfig selectById(Integer id) throws SQLException {
		// TODO 德龙
		String sql = "select config_id,config_name,config_type,goods_id,redirect_url,config_rank,is_deleted,create_time,create_user,update_time,update_user from index_config  where config_id = ?";
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				IndexConfig i = new IndexConfig();
				i.setConfigId(rs.getInt(1));
				i.setConfigName(rs.getString(2));
				i.setConfigType(rs.getInt(3));
				i.setGoodsId(rs.getInt(4));
				i.setRedirectUrl(rs.getString(5));
				i.setConfigRank(rs.getInt(6));
				i.setIsDeleted(rs.getBoolean(7));
				i.setCreateTime(rs.getDate(8).toLocalDate());
				i.setCreateUser(rs.getInt(9));
				i.setUpdateTime(rs.getDate(10).toLocalDate());
				i.setUpdateUser(rs.getInt(11));
				return i;
			}
			return null;
		}
	}

	@Override
	public Integer selectCount() throws SQLException {
		// 不实现
		return null;
	}

	@Override
	public List<IndexConfig> selectByPage(QueryCondition condition) throws SQLException {
		// TODO 严艺晗
		int page = condition.getPage();
		int pageSize = condition.getPageSize();
		String sql = "select config_id,config_name,config_type,goods_id,redirect_url,config_rank,is_deleted,create_time,create_user,update_time,update_user from index_config limit ?,?";
		List<IndexConfig> indexConfigs = new ArrayList<>();
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setInt(1, (page - 1) * pageSize);
			ps.setInt(2, pageSize);

			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				IndexConfig ic = new IndexConfig();
				ic.setConfigId(rs.getInt(1));
				ic.setConfigName(rs.getString(2));
				ic.setConfigType(rs.getInt(3));
				ic.setGoodsId(rs.getInt(4));
				ic.setRedirectUrl(rs.getString(5));
				ic.setConfigRank(rs.getInt(6));
				ic.setIsDeleted(rs.getBoolean(7));
				ic.setCreateTime(rs.getDate(8).toLocalDate());
				ic.setCreateUser(rs.getInt(9));
				ic.setUpdateTime(rs.getDate(10).toLocalDate());
				ic.setUpdateUser(rs.getInt(11));
				indexConfigs.add(ic);
			}
		}
		return indexConfigs.isEmpty() ? Collections.emptyList() : indexConfigs;
	}

	@Override
	public Integer selectTotalIndexConfig(QueryCondition condition) throws SQLException {
		// TODO 严艺晗
		String sql = "SELECT COUNT(*) FROM index_config where is_deleted=0  ";
		if (condition != null && condition.get("configType") != null && condition.get("configType") != "") {
			sql += " and config_type = ?";
		}
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			if (condition != null && condition.get("configType") != null && condition.get("configType") != "") {
				ps.setObject(1, condition.get("configType"));
			}
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		}
		return null;
	}

	@Override
	public Integer deleteBatch(Integer[] ids) throws SQLException {
		// TODO 董发良
		String sql = "update index_config set is_deleted = 1 where is_deleted = 0 and config_id "
				+ SQLHandler.joinIn(ids);

		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			int row = ps.executeUpdate();
			return row;
		}
	}

	@Override
	public List<IndexConfig> selectIndexConfigsByTypeAndNum(Integer configType, Integer number) throws SQLException {
		// TODO 董发良
		String sql = "SELECT config_id,config_name,config_type,goods_id,redirect_url,config_rank,is_deleted,create_time,create_user,update_time,update_user "
				+ " from index_config where config_type = ? and is_deleted=0  order by config_rank desc LIMIT ?";

		List<IndexConfig> IndexConfig = new ArrayList<>();
		try (Connection c = DBUtils.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setInt(1, configType);
			ps.setInt(2, number);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				IndexConfig c1 = new IndexConfig();
				c1.setConfigId(rs.getInt(1));
				c1.setConfigName(rs.getString(2));
				c1.setConfigType(rs.getInt(3));
				c1.setGoodsId(rs.getInt(4));
				c1.setRedirectUrl(rs.getString(5));
				c1.setConfigRank(rs.getInt(6));
				c1.setIsDeleted(rs.getBoolean(7));
				c1.setCreateTime(rs.getDate(8).toLocalDate());
				c1.setCreateUser(rs.getInt(9));
				c1.setUpdateTime(rs.getDate(10).toLocalDate());
				c1.setUpdateUser(rs.getInt(11));
				IndexConfig.add(c1);
			}
			return IndexConfig.isEmpty() ? Collections.emptyList() : IndexConfig;
		}
	}

}
